package com.ksv.qlsv;

import com.ksv.lop.ConnectDB;
import com.ksv.lop.MonHoc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MonHocEditActivity extends Activity {
	private TextView txt_id;
	private TextView txt_name;
	private Button btn_save;
	private Button btn_cancel;
	
	public static MonHoc mh = new MonHoc();
	public static int iAddEditView = 1;
	public static boolean bSave = false;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogedit);            
        this.setTitle(getResources().getString(R.string.monhoc));
        
        btn_save = (Button)findViewById(R.id.btn_save);
        btn_save.setText(getResources().getString(R.string.luu));
        
		btn_cancel = (Button)findViewById(R.id.btn_cancel);
		btn_cancel.setText(getResources().getString(R.string.huy));
		
        txt_id = (TextView)findViewById(R.id.txt_id);
        
        txt_name = (TextView)findViewById(R.id.txt_name);   
        
        if(iAddEditView == 1){
        	mh.setMaMon(ConnectDB.getMonHocMax());
        	txt_id.setText(String.valueOf(mh.getMaMon()));
        	txt_id.setEnabled(false);
        	txt_name.setText("");
        	txt_name.setEnabled(true);
        }
        else if(iAddEditView == 2){
        	txt_id.setText(String.valueOf(mh.getMaMon()));        
        	txt_id.setEnabled(false);
        	txt_name.setText(mh.getTenMon());
        }
        else{
        	txt_id.setText(String.valueOf(mh.getMaMon()));        
        	txt_id.setEnabled(false);
        	txt_id.setEnabled(false);        	
        	txt_name.setEnabled(false);
        	btn_save.setEnabled(false);
        	txt_name.setText(mh.getTenMon());
        }
        
        onClickButtonSave();
        
        onClickButtonCancel();  
        
    }

	private void onClickButtonSave() {
		// TODO Auto-generated method stub		
		btn_save.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bSave = true;
				if(iAddEditView == 1){
					mh.setTenMon(txt_name.getText().toString());
					ConnectDB.AddMonHoc(mh);
					MonHocEditActivity.this.finish();
				}
				else if(iAddEditView == 2){
					mh.setTenMon(txt_name.getText().toString());
					ConnectDB.updateMonHoc(mh);
					MonHocEditActivity.this.finish();
				}
				else{
					MonHocEditActivity.this.finish();
				}
			}
		});
	}

	private void onClickButtonCancel() {
		// TODO Auto-generated method stub
		btn_cancel.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub		
				bSave = false;
				MonHocEditActivity.this.finish();
			}
		});		
	}
	
	
}
